// pages/basefrom/basefrom.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ["本人办理", "未成年人", "代理办理"],
    index:"0",
    status:["none","none"],
    index_status:"none",
    result:"",
    sex: [{name:'男', value:'0',checked:''},{ name: '女', value:'1', checked:'' }],
    ordernum:{},
    idname:{},
    idcard:{},
    tel:{},
    fname:'',
    fidcard:'',
    ftel:'',
    Dname:'',
    Didcard:'',
    Dtel:'',
    diyform:{}
  },
  Goscan: function () {
    var that = this;
    wx.chooseImage({
      count: 1,  //最多可以选择的图片总数   
      sizeType: ['original', 'compressed'],// 可以指定是原图还是压缩图，默认二者都有 
      success: function (res) {
        var tempFilePaths = res.tempFilePaths[0];
        wx.showLoading({
          title: '',
        });
        wx.uploadFile({
          url: app.globalData.apiurl + '/api/index/scanimg.html',
          filePath: tempFilePaths,
          name: 'images',
          formData: {},
          success: function (res) {

            var data = JSON.parse(res.data);
            var status = data['res']['image_status'];
            if (status == "normal") {
              var address = data['res']['words_result']['住址']['words'];
              var idnum = data['res']['words_result']['公民身份号码']['words'];
              var name = data['res']['words_result']['姓名']['words'];
              var sex = data['res']['words_result']['性别']['words'];
              var minzu = data['res']['words_result']['民族']['words'];

              var result = "姓名:" + name + ",身份证号码:" + idnum + ",性别:" + sex + ",民族:" + minzu + ",住址:" + address;

              var sexx= that.data.sex;
              if(sex=="男"){
                sexx[0].checked="true"
                sexx[1].checked=""
                app.globalData.sex="0"
              }
              if(sex=="女"){
                sexx[0].checked = ""
                sexx[1].checked = "true"
                app.globalData.sex ="1"
              }
              that.setData({
                idname:name,
                idcard:idnum,
                sex:sexx
              });
              app.globalData.name=name;
              app.globalData.idcard=idnum;
              wx.hideLoading();
            } else {
              wx.showToast({
                title: '识别失败',
                icon: 'none'
              })
            }

          },
        })
      }
    });

  },

  GoscanF:function(){
    var that = this;
    wx.chooseImage({
      count: 1,  //最多可以选择的图片总数   
      sizeType: ['original', 'compressed'],// 可以指定是原图还是压缩图，默认二者都有 
      success: function (res) {
        var tempFilePaths = res.tempFilePaths[0];
        wx.showLoading({
          title: '',
        });
        wx.uploadFile({
          url: app.globalData.apiurl + '/api/index/scanimg.html',
          filePath: tempFilePaths,
          name: 'images',
          formData: {},
          success: function (res) {
            var data = JSON.parse(res.data);
            var status = data['res']['image_status'];
            if (status == "normal") {
              var address = data['res']['words_result']['住址']['words'];
              var idnum = data['res']['words_result']['公民身份号码']['words'];
              var name = data['res']['words_result']['姓名']['words'];
              var sex = data['res']['words_result']['性别']['words'];
              var minzu = data['res']['words_result']['民族']['words'];
              var result = "姓名:" + name + ",身份证号码:" + idnum + ",性别:" + sex + ",民族:" + minzu + ",住址:" + address;
              that.setData({
                fname: name,
                fidcard: idnum  
              });
              app.globalData.changeform[0].Dname = name;
              app.globalData.changeform[1].Didcard = idnum;
              wx.hideLoading();
            } else {
              wx.showToast({
                title: '识别失败',
                icon: 'none'
              })
            }

          },
        })
      }
    });

  },

  GoscanD: function () {
    var that = this;
    wx.chooseImage({
      count: 1,  //最多可以选择的图片总数   
      sizeType: ['original', 'compressed'],// 可以指定是原图还是压缩图，默认二者都有 
      success: function (res) {
        var tempFilePaths = res.tempFilePaths[0];
        wx.showLoading({
          title: '',
        });
        wx.uploadFile({
          url: app.globalData.apiurl + '/api/index/scanimg.html',
          filePath: tempFilePaths,
          name: 'images',
          formData: {},
          success: function (res) {
            var data = JSON.parse(res.data);
            var status = data['res']['image_status'];
            if (status == "normal") {
              var address = data['res']['words_result']['住址']['words'];
              var idnum = data['res']['words_result']['公民身份号码']['words'];
              var name = data['res']['words_result']['姓名']['words'];
              var sex = data['res']['words_result']['性别']['words'];
              var minzu = data['res']['words_result']['民族']['words'];
              var result = "姓名:" + name + ",身份证号码:" + idnum + ",性别:" + sex + ",民族:" + minzu + ",住址:" + address;
              that.setData({
                Dname: name,
                Didcard: idnum
              });
              app.globalData.changeform[3].name = name;
              app.globalData.changeform[4].idcard = idnum;
              wx.hideLoading();
            } else {
              wx.showToast({
                title: '识别失败',
                icon: 'none'
              })
            }

          },
        })
      }
    });

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var _this = this
    wx.request({
      url: app.globalData.apiurl + '/api/index/modify',
      method:"GET",
      data:{
        ordernum:e.ordernum
      },
      success:function(res){
        var sexx = _this.data.sex;
        if (res.data.sqrsex == "0") {
          sexx[0].checked = "true"
          sexx[1].checked = ""
          app.globalData.sex = "0"
        }
        if (res.data.sqrsex == "1") {
          sexx[0].checked = ""
          sexx[1].checked = "true"
          app.globalData.sex = "1"
        }
        if (res.data.dltype == 1) {
          var sta = ['none', 'none']
          sta[0] = '';
          _this.setData({
            index_status: '',
            status: sta,
            fname: res.data.dlrname,
            fidcard: res.data.dlridcard,
            ftel: res.data.dlrtel,
          })
        }
        if (res.data.dltype == 2) {
          var sta = ['none', 'none']
          sta[1] = '';
          _this.setData({
            index_status: '',
            status: sta,
            Dname: res.data.dlrname,
            Didcard: res.data.dlridcard,
            Dtel: res.data.dlrtel,
          })
        }
        _this.setData({
          idname:res.data.sqrname,
          idcard:res.data.sqridcard,
          tel:res.data.sqrtel,
          index:res.data.dltype,
          diyform:res.data.diyform,
          sex:sexx,
          ordernum:e.ordernum
        })
        app.globalData.name = res.data.sqrname;
        app.globalData.idcard = res.data.sqridcard;
        app.globalData.sex = res.data.sqrsex;
        app.globalData.phone = res.data.sqrtel;
        if(res.data.dltype=="1"){
          app.globalData.changeform[0].Dname = res.data.dlrname;
          app.globalData.changeform[1].Didcard = res.data.dlridcard,
          app.globalData.changeform[2].Dphone = res.data.dlrtel
        }
        if(res.data.dltype=="2"){
          app.globalData.changeform[3].name = res.data.dlrname;
          app.globalData.changeform[4].idcard = res.data.dlridcard,
            app.globalData.changeform[5].phone = res.data.dlrtel
        }  
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  bindPickerChange: function(e){
    var _this = this;
    console.log(e.detail.value)
    if (e.detail.value == 0){
      var sta = ['none', 'none']
      _this.setData({
        index:e.detail.value,
        status:sta
      })
    }
    if(e.detail.value==1){
      var sta=['none','none']
      sta[0]='';
      _this.setData({
        index_status:'',
        index:e.detail.value,
        status:sta
      })
    }
    if(e.detail.value==2){
      var sta = ['none', 'none']
      sta[1] = '';
      _this.setData({
        index_status: '',
        index: e.detail.value,
        status: sta
      })
    }
  },

 // 信息输入
  changeInput:function(e){
    var _this = this;
    if(e.detail.value)
    app.globalData.change=e.detail.value 
    console.log(app.globalData.change)
  },

  nameInput:function(e){
    app.globalData.name=e.detail.value
  
  },
  idcardInput:function(e) {
    app.globalData.idcard= e.detail.value
  },

  bindradio:function(e){
    app.globalData.sex=e.detail.value
  },

  phoneInput:function(e){
    app.globalData.phone= e.detail.value
  },
  FnameInput:function(e){
    app.globalData.changeform[0].Dname = e.detail.value
    console.log(app.globalData.changeform[0].Dname)
  },
  FidcardInput: function (e) {
    app.globalData.changeform[1].Didcard = e.detail.value
  },
  FphoneInput: function (e) {
    app.globalData.changeform[2].Dphone = e.detail.value
  },
  DnameInput: function(e) {
    app.globalData.changeform[3].name = e.detail.value
  },
  DidcardInput: function (e) {
    app.globalData.changeform[4].idcard = e.detail.value
  },
  DphoneInput: function (e) {
    app.globalData.changeform[5].phone = e.detail.value
  },
  

//填写判断  
  bindButton:function(){
    var _this = this
    if (app.globalData.name == '') {
      wx.showToast({
        title: '请填写姓名',
        icon: 'none'
      })
      return false;
    }
    if (app.globalData.idcard == '') {
      wx.showToast({
        title: '请填写身份证号',
        icon: 'none'
      })
      return false;
    }
    if (app.globalData.sex == '') {
      wx.showToast({
        title: '请选择性别',
        icon: 'none'
      })
      return false;
    }
    if(app.globalData.phone == ''){
      wx.showToast({
        title: '请填写电话',
        icon:'none'
      })
      return false;
    }

    if(_this.data.index==1){
        if (app.globalData.changeform[0].Dname==''){
          wx.showToast({
            title: '请填写法定代理人姓名',
            icon: 'none'
          })
          return false;
        } 
      if (app.globalData.changeform[1].Didcard == ''){
          wx.showToast({
            title: '请填写法定代理人身份证号',
            icon: 'none'
          })
          return false;
        }
      if (app.globalData.changeform[2].Dphone == '') {
          wx.showToast({
            title: '请填写法定代理人手机号',
            icon: 'none'
          })
          return false;
        }
      }
      if(_this.data.index == 2){
        if (app.globalData.changeform[3].name == '') {
          wx.showToast({
            title: '请填写代理人姓名',
            icon: 'none'
          })
          return false;
        } 
        if (app.globalData.changeform[4].idcard == '') {
          wx.showToast({
            title: '请填写代理人身份证号',
            icon: 'none'
          })
          return false;
        } 
        if (app.globalData.changeform[5].phone == '') {
          wx.showToast({
            title: '请填写代理人电话',
            icon: 'none'
          })
          return false;
        } 
      }
    

   wx.navigateTo({
     url: '../x_diyform/diyform?diyform='+_this.data.diyform+'&ordernum='+_this.data.ordernum+'&index='+_this.data.index,
      success:function(){
        console.log(app.globalData.changeform[0].Dname)
      }
   })
  }
})